/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.escom.healthlc.controller;

import com.escom.healthlc.business.MedicoBusiness;
import com.escom.healthlc.entidad.MedicoVO;
import com.escom.healthlc.entidad.UsuarioVO;
import com.escom.healthlc.imapperdao.IMedicoMapper;
import com.escom.healthlc.utils.Constantes;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;

/**
 *
 * @author LAURA
 */
@ManagedBean(name = "CuentaMedicoController")
@ViewScoped
public class CuentaMedicoController {

    private static final Logger logger = LoggerFactory.getLogger(CuentaMedicoController.class);
    private MedicoVO medicoCreate;

    @ManagedProperty("#{MedicoBusiness}")
    private MedicoBusiness medicoBusiness;
    private MedicoVO retrieveMedico;
    private UsuarioVO usuario;

    public CuentaMedicoController() {
    }

    @PostConstruct
    private void initCuentaMedicoController() {
        medicoCreate = new MedicoVO();
        getUsuarioSession();
    }

    public void executeActualizarCuentaMedico(ActionEvent acEvt) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        FacesMessage facesMessage;
        medicoBusiness.actualizarMedico(retrieveMedico);
        int estado = medicoBusiness.getEstado();
        String messageEstado = medicoBusiness.getMessageEstado();
        switch (estado) {
            case Constantes.SIN_ERROR:
                facesMessage = new FacesMessage(FacesMessage.SEVERITY_INFO, Constantes.MESSAGE_TITLE, messageEstado);
                facesContext.addMessage(null, facesMessage);
                break;
            case Constantes.ERROR_BASE_DATOS:
                facesMessage = new FacesMessage(FacesMessage.SEVERITY_INFO, Constantes.MESSAGE_TITLE, messageEstado);
                facesContext.addMessage(null, facesMessage);
                break;
        }
    }

    public void getUsuarioSession() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        HttpServletRequest request = (HttpServletRequest) facesContext.getExternalContext().getRequest();
        HttpSession httpSession = request.getSession(true);
        retrieveMedico = (MedicoVO) httpSession.getAttribute("usuarioActivio");
        logger.debug("objeto Medico {}", retrieveMedico.getUsuario().toString());
    }

    public MedicoBusiness getMedicoBusiness() {
        return medicoBusiness;
    }

    public void setMedicoBusiness(MedicoBusiness medicoBusiness) {
        this.medicoBusiness = medicoBusiness;
    }

    public MedicoVO getMedicoCreate() {
        return medicoCreate;
    }

    public void setMedicoCreate(MedicoVO medicoCreate) {
        this.medicoCreate = medicoCreate;
    }

    public MedicoVO getRetrieveMedico() {
        return retrieveMedico;
    }

    public void setRetrieveMedico(MedicoVO retrieveMedico) {
        this.retrieveMedico = retrieveMedico;
    }

    public UsuarioVO getUsuario() {
        return usuario;
    }

    public void setUsuario(UsuarioVO usuario) {
        this.usuario = usuario;
    }

}
